<template>
  <vmForm :formData="formData" @confirm="confirm"></vmForm>
</template>

<script>
import vmForm from '../component/form.vue';
export default {
  components: {
    vmForm
  },
  data() {
    return {
      formData: {
        permission_name: '',
        type: '3',
        sidebar_name: '',
        icon: '',
        route_name: '',
        meta_title: '',
        route_path: '',
        component_path: '',
        pid: [],
        api_path: '',
        order: '',
        view_name: '', // 此字段原权限管理系统为命名视图，现作为按钮名称使用
        redirect: '',
        hl_routes: '',
        /*  */
        isMainMenu: ''
      }
    }
  },
  activated() {
    if (this.$route.query?.action != 'refresh' || this.$isFirstInPage.get()) {
      this.formData = this.$options.data().formData
      this.getDetail()
    }
  },
  methods: {
    async getDetail() {
      try {
        let res = await this.request({
          url: '/admin/permission/detail',
          method: 'get',
          data: {
            id: this.$route.query.id
          }
        })
        let data = res.content
        data.isMainMenu = data.pid - 0 === 0 ? 1 : 2
        data.api_path = data.api_path.replace(/,/g, '\n')
        this.formData = data
      } catch (err) {
        this.formData = this.$options.data().formData
        this.errorHandler(err)
      }
    },
    async confirm(data) {
      try {
        let res = await this.request({
          url: '/admin/permission/change',
          method: 'post',
          data: {
            ...data,
            change_time: undefined,
            create_time: undefined,
            path_name: undefined
          }
        })
        this.$message.success(res.msg)
        let refresh = ''

        setTimeout(() => {
          this.pageTurnParams('AuthorityList')
        }, 1000)
      } catch (err) {
        this.errorHandler(err)
      }
    }
  }
}
</script>

